<?php


namespace app\wap\model;

use think\Model;

// 队列
class Queue extends Model
{
    protected $queue   = [];// 存储队列
    protected $maxsize = 4; // 最大数

    // todo 注释后会报数据库连接错误
    public function __construct()
    {

    }

    // 判断队空
    public function QIsEmpty()
    {
        return count($this->queue) == 0 ? 1 : 0;
    }

    // 判断队满
    public function QIsFull()
    {
        return count($this->queue) == $this->maxsize ? 1 : 0;
    }

    // 读取队列
    public function getDate($readNum = 1)
    {
        $data = [];
        if (!$this->QIsEmpty()){
            // 一次取出多少个值， 从0开始取
            $data = array_slice($this->queue, 0, $readNum);
        }
        return $data;
    }

    // 入队
    public function InQ($data)
    {
        if ($this->QIsFull()) {
            echo "<br>";
            print_r($data);
            echo ":我一来咋就满了！（队满不能入队，请等待！）<br>";
            $status = 0;
        } else {
            $this->queue[] = $data;
            echo '<br>';
            print_r($data);
            echo '入队成功<br>';
            $status = 1;
        }
        return $status;
    }

    // 出队
    public function OutQ()
    {
        if ($this->QIsEmpty()) {
            echo "队空！<br>";
        } else {
            $first = $show = array_shift($this->queue);
            unset($first);
            echo '<br>';
            print_r($show);
            echo '出队成功<br>';
        }
    }








}